<template>
  <div class="app-container">
    <!-- 顶部统计 -->
    <analyze></analyze>
    <el-form :model="queryParams" ref="queryForm" size="mini"  v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="单据类型" prop="oddType">
            <el-select v-model="queryParams.oddType" placeholder="请选择">
              <el-option key="1" label="采购单" :value="1"></el-option>
              <el-option key="2" label="销售退货单" :value="2"></el-option>
              <el-option key="3" label="采购退换" :value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="单据编号" prop="oddNo">
            <el-input v-model="queryParams.oddNo" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="供应商" prop="businessName">
            <el-input v-model="queryParams.params.businessName" placeholder="请输入" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="单据日期" prop="oddDate">
            <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
              range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="付款状态" prop="oddStatus">
            <el-select v-model="queryParams.oddStatus" placeholder="请选择">
              <el-option key="0" label="未付款" :value="0"></el-option>
              <el-option key="1" label="已付款" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['erp:ErpAccountsPayable:export']">列表导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="ErpAccountsPayableList"  row-key="payableId" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
      <el-table-column label="单据编号" align="center" prop="oddNo" sortable/>
      <el-table-column label="供应商/客户" align="center" prop="supplierName"/>
      <el-table-column label="单据日期" align="center" prop="oddDate" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oddDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="oddType" sortable width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.oddType == 1">采购单</el-tag>
          <el-tag v-if="scope.row.oddType == 2">销售退货单</el-tag>
          <el-tag v-if="scope.row.oddType == 3">采购退换</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="linkman" width="100"/>
      <el-table-column label="联系电话" align="center" prop="linkmanPhone" width="150"/>
      <el-table-column label="付款状态" align="center" prop="oddStatus" sortable width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.oddStatus == 0" type="danger">未付款</el-tag>
          <el-tag v-if="scope.row.oddStatus == 1" type="success">已付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="oddSumMoney" sortable width="120">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.oddSumMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款金额" align="center" prop="oddMoney" sortable width="120">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.oddMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款时间" align="center" prop="payTime" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.oddStatus == 0" type="text"
            @click="handlePayment(scope.row)" v-hasPermi="['erp:ErpAccountsPayable:pay']">付款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listErpAccountsPayable, paymentErpAccountsPayable } from "@/api/erp/ErpAccountsPayable";
import analyze from "./analyze.vue";
import {formattedNumber} from "@/utils/ruoyi";

export default {
  name: "ErpAccountsPayable",
  components: {
    analyze
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应付账款表格数据
      ErpAccountsPayableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oddNo: null,
        oddType: null,
        oddStatus: null,
        params: {
          businessName: null
        },
      },
      // 日期范围
      dateRange: [],
      // 多选ids
      ids: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formattedNumber,
    /** 查询应付账款列表 */
    getList() {
      this.loading = true;
      listErpAccountsPayable(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.ErpAccountsPayableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.payableId)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.params.businessName = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 付款按钮操作 */
    handlePayment(row) {
      this.$modal.confirm('是否确认付款？').then(function () {
        return paymentErpAccountsPayable(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("付款成功");
      }).catch(() => { });
    },
      /** 导出按钮操作 */
      handleExport() {
      this.download(
        "erp/ErpAccountsPayable/export",
        {
          ...this.queryParams,
          exportIds: this.ids?.join(',')
        },
        `应付账款_${new Date().getTime()}.xlsx`
      );
    },
  }
};
</script>
